package com.ayoka.parenting.ui.live.liveclassroom;

import android.text.TextUtils;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.BasicUserInfoBean;
import com.ayoka.parenting.bean.ChatMessageBean;
import com.ayoka.parenting.bean.DiscussGroupTempRoomBean;
import com.ayoka.parenting.bean.GrownClassroomDetailBean;
import com.ayoka.parenting.bean.LiveClassroomDetailBean;
import com.ayoka.parenting.http.API;
import com.ayoka.parenting.http.HttpBeanInterceptCallback;
import com.ayoka.parenting.interf.CallbackUtil;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.mvp.BasePresenter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: LiveClassroomModel&.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class LiveClassroomModel implements LiveClassroomContract.Model {

    private List<String> courseStepList = new ArrayList<>(Arrays.asList("0.准备工作", "1.介绍当日主题", "2.两两讨论", "3.提醒学员要点", "4.教练带冥想", "5.团体讨论冥想体验", "6.Q&A问答环节", "7.总结关键点"));

    private List<BasicUserInfoBean> memberList;

    private List<ChatMessageBean> chatMessageList;

    @Override
    public void enterLiveRoom(final BasePresenter presenter, final String scheduleId, final RequestCallback<GrownClassroomDetailBean> callback) {
        API.enterLiveRoom(presenter, scheduleId, new HttpBeanInterceptCallback<BaseResponse<LiveClassroomDetailBean>>() {
            @Override
            public void onSuccess(BaseResponse<LiveClassroomDetailBean> response) {
                CallbackUtil.notifySuccess(callback, response.data.schedule);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    enterLiveRoom(presenter, scheduleId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void getScheduleTempSubRoom(final BasePresenter presenter, final String scheduleId, final RequestCallback<BaseResponse<DiscussGroupTempRoomBean>> callback) {
        API.GetScheduleTempSubRoom(presenter, scheduleId, new HttpBeanInterceptCallback<BaseResponse<DiscussGroupTempRoomBean>>() {
            @Override
            public void onSuccess(BaseResponse<DiscussGroupTempRoomBean> response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getScheduleTempSubRoom(presenter, scheduleId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }


    private void getMemberData(final LiveClassroomModel liveClassroomModel, final String userId, final RequestCallback<BasicUserInfoBean> callback) {
        API.getMemberData(liveClassroomModel, userId, new HttpBeanInterceptCallback<BaseResponse<BasicUserInfoBean>>() {
            @Override
            public void onSuccess(BaseResponse<BasicUserInfoBean> response) {
                notifyMember(response.data);
                notifyMessageMember(response.data);
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_CODE) {
                    getMemberData(liveClassroomModel, userId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }


    @Override
    public List<String> getCourseStepList() {
        return courseStepList;
    }


    @Override
    public BasicUserInfoBean getMemberByUserId(String userId, boolean isNeedAdd) {
        BasicUserInfoBean result = null;
        for (BasicUserInfoBean userBean : getMemberList()) {
            if (userBean == null) continue;
            if (TextUtils.equals(userId, String.valueOf(userBean.ID))) {
                result = userBean;
                break;
            }
        }
        if (result == null && isNeedAdd) {
            getMemberList().add(new BasicUserInfoBean(userId, userId, ""));
            getMemberData(this, userId, null);
        }
        return result;
    }

    @Override
    public void saveChatMessage(ChatMessageBean chatMessageBean) {
        getChatMessageList().add(chatMessageBean);
    }

    @Override
    public void saveChatMessage(int index, ChatMessageBean chatMessageBean) {
        getChatMessageList().add(index, chatMessageBean);
    }

    @Override
    public BasicUserInfoBean removeMemberByUserId(String userId) {
        BasicUserInfoBean userBean = getMemberByUserId(userId, false);
        if (userBean != null) {
            getMemberList().remove(userBean);
        }
        return userBean;
    }


    public List<BasicUserInfoBean> getMemberList() {
        if (memberList == null) {
            memberList = new ArrayList<>();
        }
        return memberList;
    }

    public List<ChatMessageBean> getChatMessageList() {
        if (chatMessageList == null) {
            chatMessageList = new ArrayList<>();
        }
        return chatMessageList;
    }

    private void notifyMessageMember(BasicUserInfoBean data) {
        List<ChatMessageBean> chatMessageList = getChatMessageList();
        for (ChatMessageBean chatMessageBean : chatMessageList) {
            if (chatMessageBean.userBean != null && TextUtils.equals(chatMessageBean.userBean.ID, data.ID)) {
                chatMessageBean.userBean = data;
            }
        }
    }

    public void notifyMember(BasicUserInfoBean userBean) {
        if (!getMemberList().contains(userBean)) {
            getMemberList().add(userBean);
        } else {
            int index = getMemberList().indexOf(userBean);
            getMemberList().set(index, userBean);
        }
    }
}
